package com.example.demo.web.ctrl;

import com.example.demo.Request;
import com.example.demo.grpc.utils.MsgBuilder;
import com.example.demo.hello.Bb;
import com.example.demo.hello.HelloRequest;
import com.example.demo.hello.HelloResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/")
public class IndexCtl {

    @GetMapping(value = "/grpc", produces = MediaType.APPLICATION_PROTOBUF_VALUE)
    public HelloResponse grpc() {
        return getBuild();
    }

    @GetMapping(value = "/hello" ,consumes =  MediaType.APPLICATION_PROTOBUF_VALUE,produces = MediaType.APPLICATION_PROTOBUF_VALUE)
    public HelloResponse hello(HelloRequest helloRequest) {
        return getBuild();
    }

    @GetMapping(value = "/grpcBuild", produces = MediaType.APPLICATION_PROTOBUF_VALUE)
    public Request grpcBuild() {
        return MsgBuilder.buildReq(getBuild());
    }

    @GetMapping("text")
    public String text() {
        return "hello world";
    }

    private static HelloResponse getBuild() {
        return HelloResponse.newBuilder()
                .setMessage("INFO  c.e.d.g.u.ServerLogInterceptor1 - [onMessage,63] - 方法名:com.example.demo.HelloService/sayHello,接收:{\"name\":\"lis\",\"aa\":{\"a1\":\"a1\",\"a2\":\"a2\"}}")
                .setBb(Bb.newBuilder()
                        .setB1("Registered gRPC service: com.example.demo.HelloService, bean: helloServiceGrpc, class: com.example.demo.grpc.service.HelloServiceGrpc")
                        .setB2("Registered gRPC service: com.example.demo.TerminalService, bean: terminalSeriveGrpc, class: com.example.demo.grpc.service.TerminalSeriveGrpc")
                        .build())
                .build();
    }
}
